<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import gdglService from '@/api/gdgl'
import { useRoute } from 'vue-router'
const route = useRoute()
const emit = defineEmits(['refresh'])

const isLoading = ref(false)
const ruleFormRef = ref<FormInstance>()

//打开窗口

const dialogVisible = ref(false)
const nodeData = ref()

const openDialog = (file: any) => {
  nodeData.value = file
  dialogVisible.value = true
}

const xzList = ref([
  {
    value: 90,
    label: '90°',
  },
  {
    value: 180,
    label: '180°',
  },
  {
    value: 270,
    label: '270°',
  },
])

const ruleForm = ref({
  xzjd: '',
})

/* 表单校验 */
const rules = ref<FormRules>({
  xzjd: [
    {
      required: true,
      message: '请输入',
      trigger: 'change',
    },
  ],
})

const save = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      const formData = new FormData()
      formData.append('file', nodeData.value)
      formData.append('xzjd', ruleForm.value.xzjd)
      isLoading.value = true
      gdglService
        .getImgXz(formData, route.query.ajbh as string)
        .then((res) => {
          closeDialog()
          emit('refresh', res)
        })
        .catch((err) => {
          !err.isError && ElMessage({ type: 'warning', message: err.message })
        })
        .finally(() => {
          isLoading.value = false
        })
    }
  })
}

/*
 * 关闭弹框
 */
const closeDialog = () => {
  dialogVisible.value = false
}

defineExpose({ openDialog })
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    title="旋转文件"
    width="25%"
    :close-on-click-modal="false"
    :before-close="closeDialog"
  >
    <el-form ref="ruleFormRef" label-width="auto" :model="ruleForm" :rules="rules">
      <el-form-item label="旋转角度" prop="xzjd" style="width: 100%">
        <el-select v-model="ruleForm.xzjd" clearable placeholder="请选择" style="width: 100%">
          <el-option
            v-for="zrritem in xzList"
            :key="zrritem.value"
            :label="zrritem.label"
            :value="zrritem.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="save(ruleFormRef)" :loading="isLoading">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style></style>
